package io.github.cisumer.wcfbin4j.nodes.texts;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import io.github.cisumer.wcfbin4j.Node;
import io.github.cisumer.wcfbin4j.NodeFactory;
import io.github.cisumer.wcfbin4j.nodes.TextRecord;
import io.github.cisumer.wcfbin4j.nodes.others.EndElement;

public class StartListText extends EndElement{
	private List<Node> contents=new ArrayList<Node>();
	
	public StartListText addContent(TextRecord<?> text){
		contents.add(text);
		return this;
	}
	public StartListText end(){
		contents.add(new EndListText());
		return this;
	}
	@Override
	public int getType() {
		return 0xA4;
	}

	@Override
	public void parse(InputStream is) throws IOException {
		int type=-1;
		while((type=is.read())!=-1){
			Node t=NodeFactory.createNode(type);
			t.parse(is);
			contents.add(t);
			if(EndElement.isEnd(type))
				break;
		}
	}

	@Override
	public void renderContent(OutputStream os) throws Exception {
		if(contents!=null && contents.size()>0){
			contents.forEach(n->n.toBytes(os));
		}
	}

	public String toXML(){
		StringBuffer sb=new StringBuffer();
		if(contents!=null && contents.size()>0){
			sb.append("\r\n");
			sb.append(String.join("\r\n",contents.toArray(new String[0])));
			sb.append("\r\n");
		}
		return sb.toString();
	}
	
}
